/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FileBrowser;

import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;
import java.util.*;



/**
 *
 * @author thomas.s
 */
public class MediaBrowser extends Canvas {

    FileBrowser browser;

    String dir, info;
    Vector list;
    int curr;

    MediaPlayer pl=null;
    Image bg=null;

    public double duration, position;
    public boolean paused, enabled, paintEnable=false;

    private Timer timer;
    int y;

    class updateTask extends TimerTask {
        private MediaBrowser canvas;
        updateTask( MediaBrowser c ){  canvas = c;  }
        public void run() {  if (canvas.paintEnable) canvas.repaint(); }
    }
    MediaBrowser( FileBrowser _browser, MediaPlayer p, String _dir, Vector _list, int _curr )
    {
        browser = _browser;
        pl = p;
        dir = _dir;
        list = _list;
        curr = _curr;
        open();
        timer = new Timer();
        timer.schedule(new updateTask(this), 10, 300 );
    }


    public void startImage( Image b, String i )
    {
        paintEnable=true;
        bg = b;
        info = i;
        repaint();
    }

    public void startAudio()
    {
        paintEnable=true;
    }

    public void startVideo( VideoControl vc )
    {
        vc.initDisplayMode(vc.USE_DIRECT_VIDEO, this);
        try { vc.setDisplaySize(480,400); } catch( MediaException e ) {}
        vc.setDisplayLocation(0,0);

    }

    public void startPlaying( double d )
    {
        duration = d;
    }

    public boolean open()
    {
        paintEnable = false;
        enabled = false;
        pl.open( this, list.elementAt(curr).toString(), "file:///" + dir + list.elementAt(curr).toString());
        return true;
    }


    void close()
    {
        enabled = false;
        pl.close();
    }

    boolean next()
    {
        enabled = false;
        curr++;
        if (curr>=list.size()) curr=0;
        if (open()) return true;
        return false;
    }

    public void paint( Graphics g)
    {
        int w = getWidth();
        int h = getHeight();
        g.setColor(0x101020);


        int y = pl.videoRunning()?400:0;
        if (bg!=null) { g.fillRect(0,0,w,h); g.drawImage(bg,0,0,0); } 
        else { g.fillRect(0,y,w,h-y); }

        position = pl.pos();
        String p = pl.sPos;
        
        g.setFont( Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM));

        if (duration>0) {
            g.setColor(0x8080f0);
            g.drawRect( 10,400,w-20,50);
            g.setColor(0x404080);
            g.fillRect( 11,401, (int)((position*(double)(w-22))/duration), 48);
            g.setColor(0xa0a0f0);
            g.drawString( p + " / " + duration + "s " ,22,410,0);

        } else
        if (bg==null) {
            g.setColor(0xa0a0a0);
            g.drawString( p ,22,410,0);
        }

        g.setFont( Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_SMALL));

        if (bg!=null && info!=null) g.drawString(info,0,0,0);

        for ( int i=0; i<10 && i<=list.size(); i++ )
        {
            int ind = (i+curr+list.size()-1)%list.size();
            if (i>=1 || list.size()>1) 
            {
                String text = list.elementAt(ind).toString();
                g.setColor(0);
                g.drawString( text,1,450+i*30,0);
                g.drawString( text,-1,450+i*30,0);
                g.drawString( text,0,449+i*30,0);
                g.drawString( text,0,451+i*30,0);
                if (i==1) g.setColor(0xf0f080); else g.setColor(0x8080f0);
                g.drawString( text,0,450+i*30,0);
            }
        }
    }

    public void keyPressed( int key )
    {
        if (key==-10) next();
    }
    public void pointerPressed( int x, int y )
    {
         if (enabled)
         {
            int w = getWidth();
                if ( y<200 ) next(); else
                if ( y<400 ) {
                    pl.pause();
                } else
                if ( y>400 && y<450 && duration>0) 
                {
                  //  paintEnable=false;
                    pl.setPos(((double)x)/(double)w);
                   // paintEnable=true;
                } else
                if ( y>450 ) { int p = (y-450)/30 - 1; curr+=p; if (p!=0) open(); }
         }
    }
}
